package com.utils.mailUtil;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SimpleMailSender implements Runnable {

	MailSenderInfo mailInfo;

	public static boolean sendMail(MailSenderInfo mailInfo) {
		// 判断是否需要身份认证
		MyAuthenticator authenticator = null;
		// 如果需要身份认证，则创建一个密码验证器
		if (mailInfo.isValidate()) {
			authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		// 用邮件服务器地址，端口，是否需要身份验证，构造一个Properties
		Properties pro = mailInfo.getProperties();
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getInstance(pro, authenticator);
		// 开启Session的debug模式，可以查看到程序发送Email的运行状态
		sendMailSession.setDebug(mailInfo.isDebug());
		try {
			// 根据session创建一个邮件消息
			Message mailMessage = new MimeMessage(sendMailSession);
			// 创建邮件发送者，设置邮件发送者
			Address from = new InternetAddress(mailInfo.getFromAddress(), mailInfo.getFromUserName());
			mailMessage.setFrom(from);
			// 创建邮件接收者，设置邮件接收者
			Address to = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, to);
			// 设置客户端显示的发信时间
			mailMessage.setSentDate(new Date());
			// 设置邮件消息的主题
			mailMessage.setSubject(mailInfo.getSubject());

			if (mailInfo.getHtmlMail()) {
				// MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
				Multipart mainPart = new MimeMultipart();
				// 创建一个包含HTML内容的MimeBodyPart
				BodyPart mimeBody = new MimeBodyPart();
				// 设置HTML内容
				mimeBody.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
				mainPart.addBodyPart(mimeBody);
				// 将MiniMultipart对象设置为邮件内容
				mailMessage.setContent(mainPart);
			} else {
				// 设置文本邮件消息的主要内容
				mailMessage.setText(mailInfo.getContent());
			}

			// 发送邮件
			Transport.send(mailMessage);
			return true;
		} catch (MessagingException ex) {
			ex.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 描述：开启新线程发送邮件
	 * 
	 * @param mailInfo
	 */
	public static void startMailThread(MailSenderInfo mailInfo) {
		SimpleMailSender myThread = new SimpleMailSender();
		myThread.setMailInfo(mailInfo);
		new Thread(myThread).start();
	}

	public static void sendTextMail(MailSenderInfo mailInfo) {
		// 设置邮件服务器信息
		mailInfo.setMailServerHost("smtp.ym.163.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);

		// 邮箱用户名,密码
		mailInfo.setUserName("info@bidiam.com");
		mailInfo.setPassword("6FHJKOab6GxLxIeh");

		// 发件人
		mailInfo.setFromAddress("info@bidiam.com");
		mailInfo.setFromUserName("BIDIAM好");
		
		//html邮件
//		mailInfo.setHtmlMail(true);

		// 收件人
//		mailInfo.setToAddress("49834668@qq.com");

		// 邮件标题,内容
//		mailInfo.setContent("content");
//		mailInfo.setSubject("subject");

		// 开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
//		mailInfo.setDebug(true);
		
		//添加签名
		StringBuilder builder=new StringBuilder(mailInfo.getContent());
		builder.append("\n\n\n\n--------------------------------------------------");
		builder.append("\n\tBIDIAM");
		builder.append("\n\n\thttp://www.bidiam.com");
		builder.append("\n\t50-52 Hovenierstraat B 2018 ANTWERP BELGIUM");
		builder.append("\n\tTEL：+32 3298 4427");
		builder.append("\n\tCustomer Service：+852 56060277");
		builder.append("\n\tSkype：info@bidiam.com");
		mailInfo.setContent(builder.toString());

		SimpleMailSender.startMailThread(mailInfo);
		
		System.out.println("邮件发送完毕");
	}

	@Override
	public void run() {
		sendMail(mailInfo);
	}

	public MailSenderInfo getMailInfo() {
		return mailInfo;
	}

	public void setMailInfo(MailSenderInfo mailInfo) {
		this.mailInfo = mailInfo;
	}
}
